// pages/test/test.js
let videoAd = null
let interval = null
let interstitialAd = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time:1000,
    qiang:10/100,
    guanggao:10/100,
    guoguan:3/100,
    width:2,
    boxArr:[],
    score:0,
    title:'试试你的眼力和手速，达到什么级别了',
    ad:{},
    tiepian:''
  },
  reset(){
    let that = this
    this.setData({
      time:1000,
      qiang:10/100,
      guanggao:10/100,
      guoguan:3/100,
      width:2,
      score:0,
      title:'试试你的眼力和手速，达到什么级别了'
    })
    that.gameInit()
    interval = setInterval(()=>{
      that.gameInit()
    },this.data.time)
  },
  levelUp(){
    clearInterval(interval)
    let {width,qiang,guanggao,score,time}= this.data
    let that = this
    score++
    if(score<5){
      width = width+1
      this.setData({
        title:'你还是新手哦~~'
      })
    }
    if(score>=5&&score<=80){
      if(score%10 == 0){
        width+=1
      }
      this.setData({
        title:'距里高手还有两步的差距！'
      })
    }
    if(score>80&&score<=200){
      this.setData({
        title:'你已经快无敌了！'
      })
      if(score%10 == 0){
        width+=1
      }
    }
    if(score>200){
      this.setData({
        title:'情场高手，专业老司机！！'
      })
    }
    if(score>500){
      this.setData({
        title:'你已经突破天际了！！！'
      })
    }
    if(qiang+guanggao>0.94){
      this.setData({
        time:time-10
      })
    } else {
      qiang +=0.01
      guanggao +=0.01
    }
    
    this.setData({width,qiang,guanggao,score})
    that.gameInit()
    interval = setInterval(()=>{
      that.gameInit()
    },this.data.time)
  },
  gameInit(){
    let that = this
    let {width,qiang,guanggao,guoguan}= that.data
    let arr = []
    let total = width*width
    guanggao = total*guanggao<1?1:Math.floor(total*guanggao)
    qiang = total*qiang<1?1:Math.floor(total*qiang)
    guoguan = total*guoguan<1?1:Math.floor(total*guoguan)
    let kong = total-guanggao-qiang-guoguan
    for (let i = 0; i < kong; i++) {
      arr.push(1)
    }
    for (let i = 0; i < qiang; i++) {
      arr.push(2)
    }
    for (let i = 0; i < guanggao; i++) {
      arr.push(3)
    }
    for (let i = 0; i < guoguan; i++) {
      arr.push(4)
    }
    arr.sort(() => Math.random() - 0.5)
    this.setData({
      boxArr:arr
    })
  },
  guanggaoShow(){
    // if(Math.random() - 0.5>0){
    //   this.jili()
    // } else {
    //   this.chaping()
    // }
  },
  chaping(){
    
        
        if (interstitialAd) {
            interstitialAd.show().catch((err) => {
                console.error(err)
            })
        }
    
  },
  jili(){
    if (videoAd) {
      videoAd.show()
        .then(() => {
          wx.hideLoading({
            success: (res) => {},
          })
        })
        .catch(() => {
          wx.hideLoading({
            success: (res) => {},
          })
        })
    } else {
      wx.hideLoading({
        success: (res) => {},
      })
    }
  },
  end(){
    let that = this
    clearInterval(interval)
    wx.showModal({
      confirmText: '确定',
      content: '您已撞破墙壁，请重新开始！',
      showCancel: false,
      success:function(e){
        if(e.confirm){
          that.reset()
        }
      }
    })
    
  },
  onShow(){
    // this.getRandomAd(undefined,true)
    getApp().chaping()
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   tiepian:getApp().globalData.adTest.filter(item=>item.type == 'shipintiepian')[0].id
    // })
    let that = this
    getApp().getRandomAd(true,this)
        // this.getRandomAd(true,true)
    if (wx.createRewardedVideoAd) {
      videoAd = wx.createRewardedVideoAd({
        adUnitId: getApp().globalData.jili
      })
      videoAd.onLoad(() => { })
      videoAd.onError((err) => { })
      videoAd.onClose((status) => { 
        if (status && status.isEnded || status === undefined) {
          console.log('已完成')
        } else {
          console.log('未完成')
        }
      })
    }
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: getApp().globalData.adTest.filter(item=>{
            return item.type == 'chaping'
        })[0].id
      })
      interstitialAd.onLoad(() => {})
      interstitialAd.onError((err) => {})
      interstitialAd.onClose(() => {})
  }
    interval = setInterval(()=>{
      that.gameInit()
    },this.data.time)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(interval)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title:"快来竞争手速王~~",
      imageUrl:'/image/share2.jpg'
    }
  }
})